// @flow
codeStrength.regEx = /^[\w|[\x21-\x2F]|[\x3A-\x40]|[\x5B-\x60]|[\x7B-\x7E]]+$/;
codeStrength.normalCharacterFlag = false;
codeStrength.abnormalCharacterFlag = false;
codeStrength.figureFlag = false;
codeStrength.symbolFlag = false;

codeStrength.Secure = 'Secure';
codeStrength.Strong = 'Strong';
codeStrength.Average = 'Average';
codeStrength.Weak = 'Weak';
codeStrength.Zero = 'Zero';

function codeStrength(code: string) {
  if (!codeStrength.regEx.test(code)) {
    return 'error:您输入的密码不符合规范，请重新输入';
  }

  const point =
    codeLength(code) +
    characterOrNot(code) +
    figureOrNot(code) +
    symbolOrNot(code) +
    bouns(code);

  if (point >= 85) {
    // console.log(point, codeStrength.Secure);
    return codeStrength.Secure;
  } else if (point >= 65) {
    // console.log(point, codeStrength.Strong);
    return codeStrength.Strong;
  } else if (point >= 45) {
    // console.log(point, codeStrength.Average);
    return codeStrength.Average;
  } else if (point > 0) {
    //   console.log(point, codeStrength.Weak);
    return codeStrength.Weak;
  }
  return codeStrength.Zero;
}

// 字符串长度
function codeLength(code: string) {
  //   console.log('codeLength:');
  const { length } = code;
  if (length <= 4) {
    // console.log('   --> 5 ');
    return 5;
  } else if (length >= 5 && length <= 7) {
    // console.log('   --> 10 ');
    return 10;
  }
  //   console.log('   --> 25 ');
  return 25;
}

// 是否包含字母
function characterOrNot(code: string) {
  //   console.log('characterOrNot:');
  const regEx1 = /^[0-9\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]+$/;
  const regEx2 = /^[a-z0-9\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]+$/;
  const regEx3 = /^[A-Z0-9\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]+$/;
  if (regEx1.test(code)) {
    // 没有字母
    codeStrength.normalCharacterFlag = false;
    // console.log('    -->0');
    return 0;
  } else if (regEx2.test(code) || regEx3.test(code)) {
    // 只有小(大)写字母
    codeStrength.normalCharacterFlag = true;
    // console.log('    -->10');
    return 10;
  }
  codeStrength.abnormalCharacterFlag = true;
  //   console.log('    -->25');
  return 25;
}

function figureOrNot(code: string) {
  //   console.log('figureOrNot:');
  const reg = /[0-9]/;

  let count = 0;
  for (let index = 0; index < code.length; index += 1) {
    if (reg.test(code.charAt(index))) count += 1;
    // console.log('code.charAt(index).toString:', code.charAt(index));
  }
  switch (count) {
    case 0: // 0 分: 没有数字
      codeStrength.figureFlag = false;
      //   console.log('    -->0');
      return 0;
    case 1: // 10 分: 1 个数字
      codeStrength.figureFlag = true;
      //   console.log('     -->10');
      return 10;
    default:
      codeStrength.figureFlag = true;
      //   console.log('     -->20');
      return 20;
  }
}

function symbolOrNot(code: string) {
  //   console.log('symbolOrNot:');
  const reg = /[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]/;

  let count = 0;
  for (let index = 0; index < code.length; index += 1) {
    if (reg.test(code.charAt(index))) count += 1;
  }
  switch (count) {
    case 0: // 0 分: 没有数字
      codeStrength.symbolFlag = false;
      //   console.log('    -->0');
      return 0;
    case 1: // 10 分: 1 个数字
      codeStrength.symbolFlag = true;
      //   console.log('     -->10');
      return 10;
    default:
      codeStrength.symbolFlag = true;
      //   console.log('     -->25');
      return 25;
  }
}

function bouns() {
  //   console.log('bouns:');
  if (
    codeStrength.abnormalCharaterFlag &&
    codeStrength.figureFlag &&
    codeStrength.symbolFlag
  ) {
    // console.log('     -> 5');
    return 5;
  } else if (
    codeStrength.normalCharacterFlag &&
    codeStrength.figureFlag &&
    codeStrength.symbolFlag
  ) {
    // console.log('     -> 3');
    return 3;
  } else if (
    (codeStrength.abnormalCharaterFlag || codeStrength.normalCharacterFlag) &&
    codeStrength.figureFlag
  ) {
    // console.log('     -> 2');
    return 2;
  }

  return 0;
}
export default codeStrength;
